package cn.egame.extraction.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.egame.common.data.BaseDao;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.extraction.pojo.GameStatusInfo;
import cn.egame.interfaces.ExceptionCommon;

public class GameStatusDao extends BaseDao {
	private final static String STR_SELECCT_GAME_STATUS = "g_id, stamp_info, stamp_tag, count_terminal, count_down_total, count_down_day,count_down_week,count_down_month";
	public GameStatusDao() throws ExceptionCommonBase {
		super("egame_game");
	}

	public int setGameStatus(GameStatusInfo gameStatus)
			throws ExceptionCommonBase {
		String sql_insert = "insert into t_game_status(g_id,stamp_info, stamp_tag, count_terminal, count_down_total,"
				+ " count_down_day,count_down_week,count_down_month,count_add)"
				+ "values(%1$d,%2$d,%3$d,%4$d,%5$d,%6$d,%7$d,%8$d,0) ON DUPLICATE KEY UPDATE count_down_total = count_down_total +%5$d+count_add,"
				+ "count_down_day =  %6$d+count_add, count_down_week = %7$d+count_add,count_down_month = %8$d +count_add";
		sql_insert = String.format(sql_insert, gameStatus.getGId(),
				gameStatus.getStampInfo(), gameStatus.getStampTag(),
				gameStatus.getCountTerminal(), gameStatus.getCountDownTotal(),
				gameStatus.getCountDownDay(), gameStatus.getCountDownWeek(),
				gameStatus.getCountDownMonth());
		return this.executeUpdate(sql_insert);

	}
	
	public int updateGameStatus(GameStatusInfo gameStatus)
            throws ExceptionCommonBase {
        String sql_update = "UPDATE t_game_status SET COUNT_ONLINE_WEB = "+gameStatus.getCountOnlineWeb()+" WHERE G_ID="+gameStatus.getGId();
        return this.executeUpdate(sql_update);

    }
	

	public List<GameStatusInfo> listGameStatus() throws ExceptionCommonBase {
		List<GameStatusInfo> gameStatus = new ArrayList<GameStatusInfo>();
		String sql = "select " + STR_SELECCT_GAME_STATUS
				+ " from t_game_status ";
		Connection conn = null;
		ResultSet rs = null;
		try {
		     conn = this.getConnection();
	         rs = this.executeQuery(conn, sql);
			while (rs.next()) {
				gameStatus.add(formatGameStatus(rs));
			}
			return gameStatus;
		} catch (SQLException e) {
			throw ExceptionCommon.throwExceptionCommonBase(e);
		} finally {
			this.close(rs);
			this.releaseConnection(conn);
		}
	}
	
	
	public GameStatusInfo getGameStatus(int gId) throws ExceptionCommonBase {
		String sql = "select " + STR_SELECCT_GAME_STATUS
				+ " from t_game_status where g_id = "+gId;;
		Connection conn = null;
		ResultSet rs = null;
		try {
		     conn = this.getConnection();
	         rs = this.executeQuery(conn, sql);
			if (rs.next()) {
				return formatGameStatus(rs);
			}
			return null;
		} catch (SQLException e) {
			throw ExceptionCommon.throwExceptionCommonBase(e);
		} finally {
			this.close(rs);
			this.releaseConnection(conn);
		}
	}
	
	private GameStatusInfo formatGameStatus(ResultSet rs) throws SQLException {
        int i = 1;
        GameStatusInfo status = new GameStatusInfo();
        status.setgId(rs.getInt(i++));
        status.setStampInfo(rs.getLong(i++));
        status.setStampTag(rs.getLong(i++));
        status.setCountTerminal(rs.getInt(i++));
        status.setCountDownTotal(rs.getInt(i++));
        status.setCountDownDay(rs.getInt(i++));
        status.setCountDownWeek(rs.getInt(i++));
        status.setCountDownMonth(rs.getInt(i++));
        return status;
    }

}
